package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.IdealComment;
import com.util.DBHelp;
import com.util.RowMapper;

public class IdealCommentDao {

	DBHelp<IdealComment> help=new DBHelp<IdealComment>();
	
	public List<IdealComment> getAll() {
		String sql="SELECT `id`,`content`,`createtime`,`employeeid`,`ideaid` FROM `t_ideacomment` ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper());
	}
	
	private class Mapper implements RowMapper<IdealComment>{

		public IdealComment mapperRow(ResultSet rs) throws SQLException {
			IdealComment comment=new IdealComment();
			comment.setContent(rs.getString("content"));
			comment.setCreatetime(rs.getString("createtime"));
			comment.setEmployeeid(rs.getString("employeeid"));
			comment.setIdealid(rs.getString("ideaid"));
			comment.setId(rs.getString("id"));
			
			return comment;
		}
		
	}

	public void add(IdealComment idealComment) {
		String sql="INSERT INTO `t_ideacomment`(`id`,`content`,`createtime`,`employeeid`,`ideaid`) VALUES(?,?,?,?,?)";
		help.executeSQL(sql, idealComment.getId(),idealComment.getContent(),idealComment.getCreatetime(),idealComment.getEmployeeid(),idealComment.getIdealid());
	}

	public List<IdealComment> getByIdealId(String idealId) {
		String sql="SELECT `id`,`content`,`createtime`,`employeeid`,`ideaid` FROM `t_ideacomment` WHERE `ideaid`=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), idealId);
	}

	public void delById(String id) {
		String sql="DELETE FROM `t_ideacomment` WHERE id=?";
		help.executeSQL(sql, id);
	}

	public IdealComment getById(String id) {
		String sql="SELECT `id`,`content`,`createtime`,`employeeid`,`ideaid` FROM `t_ideacomment` WHERE `id`=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}

	public void delByICId(String id) {
		String sql="DELETE FROM `t_ideacomment` WHERE `ideaid`=?";
		help.executeSQL(sql, id);
	}
	
}
